package com.example.demo.controller;

import com.example.demo.dto.FliterExamCondition;
import com.example.demo.entity.CoachAccount;
import com.example.demo.entity.Exam;
import com.example.demo.entity.Progress;
import com.example.demo.entity.Response;
import com.example.demo.service.Impl.ExamImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/exam")
@RequiredArgsConstructor
public class ExamController {
    private final ExamImpl examImpl;
    @GetMapping("/findById/{examId}")
    public ResponseEntity<Response> findById( @PathVariable Long examId){
        try{
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("getExam", examImpl.findExam(examId)))
                            .message("查找成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("查找失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

    @GetMapping("/findByStudentID/{studentId}")
    public ResponseEntity<Response> findByStudentID( @PathVariable Long studentId){
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getExam", examImpl.findByStudentID(studentId)))
                        .message("查找成功")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @GetMapping("/findByCoachID/{coachId}")
    public ResponseEntity<Response> findByCoachID( @PathVariable Long coachId){
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("findByCoachID", examImpl.findByCoachID(coachId)))
                        .message("查找教练的所有学员考试")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @GetMapping("/findAllExam")
    public ResponseEntity<Response> findAllExam( ){
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("findAllExam", examImpl.findAllExam()))
                        .message("查找所有考试记录")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @GetMapping("/findNewExam/{studentId}")
    public ResponseEntity<Response> findNewExam(@PathVariable Long studentId ){
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("findNewExam", examImpl.findNewExam(studentId)))
                        .message("查找最新考试记录")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @PostMapping("/filterExam")
    public ResponseEntity<Response> filterExam(@RequestBody FliterExamCondition fliterExamCondition){
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("filterExam", examImpl.filterExam(fliterExamCondition)))
                        .message("筛选考试记录")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @PostMapping("/updateScore")
    public ResponseEntity<Response> updateScore(@RequestParam String score,@RequestParam Long id)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("updateScore",examImpl.updateScore(score,id)))
                        .message("填写学员成绩")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @PostMapping("/addExam")
    public ResponseEntity<Response> addExam(@RequestBody Exam exam) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addExam",examImpl.addExam(exam)))
                            .message("添加考试成绩")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("添加考试成绩失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @DeleteMapping("/deleteExam")
    public ResponseEntity<Response> deleteExam(@RequestParam Long examId) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("deleteExam",examImpl.deleteExam(examId)))
                            .message("删除考试成绩")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("删除考试成绩失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @PostMapping("/updateExam")
    public ResponseEntity<Response> updateExam(@RequestBody Exam exam) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updateExam",examImpl.updateExam(exam)))
                            .message("更新考试成绩")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("更新考试成绩失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

}
